
package io.renren.common.utils;
import java.util.ArrayList;
/**
 * 该工具类实现输入一个坐标判断该坐标是否在青岛区域内
 * @author hou
 *
 */
public class GetCord {
	private ArrayList<Double>	latArr	= new ArrayList<>();
	private ArrayList<Double>	lngArr	= new ArrayList<>();
	private double[] areaArr = 
	{ 121.284367, 36.325784, 121.251817, 36.339631, 121.176077, 36.393297, 121.133362, 36.410827, 121.110077, 36.431997, 121.091481, 36.493058, 121.098601, 36.565391, 120.975513, 36.571137, 120.941066, 36.582111, 120.891299, 36.615804, 120.879519, 36.627259, 120.865380, 36.630591, 120.823416, 36.606763, 120.817804, 36.598947, 120.798063, 36.594046, 120.788877, 36.597791, 120.781846, 36.597203, 120.777539, 36.607243, 120.767739, 36.612572, 120.758693, 36.613387, 120.753341, 36.620682, 120.746671, 36.621521, 120.738523, 36.627770, 120.721237, 36.630841, 120.716444, 36.629268, 120.712640, 36.621867, 120.703436, 36.614734, 120.699325, 36.604027, 120.693347, 36.599419, 120.679337, 36.595797, 120.662625, 36.583496, 120.653405, 36.585042, 120.639920, 36.582686, 120.637053, 36.597472, 120.647392, 36.627918, 120.660265, 36.638029, 120.662339, 36.653746, 120.651970, 36.658961, 120.655053, 36.670549, 120.638253, 36.678032, 120.630624, 36.686597, 120.616818, 36.690529, 120.611630, 36.698927, 120.599992, 36.707286, 120.597559, 36.723685, 120.588323, 36.737115, 120.581576, 36.741148, 120.567053, 36.742102, 120.548615, 36.760878, 120.547560, 36.775276, 120.571254, 36.809861, 120.607390, 36.816400, 120.608249, 36.825453, 120.615564, 36.837121, 120.599698, 36.844832, 120.590972, 36.862808, 120.580441, 36.873605, 120.580251, 36.878761, 120.583914, 36.885968, 120.607623, 36.891536, 120.623581, 36.900498, 120.625997, 36.910058, 120.625787, 36.914425, 120.621411, 36.916189, 120.606640, 36.915170, 120.585892, 36.925121, 120.580115, 36.933751, 120.572664, 36.929287, 120.568336, 36.932740, 120.575097, 36.944599, 120.564455, 36.967379, 120.576315, 36.980734, 120.576783, 37.002049, 120.601262, 37.002848, 120.611010, 37.006572, 120.605167, 37.049854, 120.602057, 37.053102, 120.587150, 37.052721, 120.584707, 37.063312, 120.562592, 37.051487, 120.551454, 37.049448, 120.547542, 37.052049, 120.541639, 37.093417, 
	  120.551266, 37.116772, 120.547805, 37.130896, 120.530365, 37.149013, 120.519523, 37.152101, 120.505948, 37.147564, 120.493157, 37.126649, 120.482904, 37.129817, 120.462566, 37.125805, 120.444689, 37.129215, 120.435135, 37.123183, 120.420306, 37.119739, 120.414138, 37.111320, 120.422792, 37.102402, 120.418265, 37.099660, 120.367436, 37.111552, 120.362262, 37.107901, 120.360396, 37.098051, 120.347315, 37.089968, 120.337630, 37.092993, 120.330869, 37.113663, 120.304600, 37.123035, 120.304209, 37.136017, 120.293572, 37.133576, 120.275765, 37.140283, 120.269999, 37.123029, 120.264639, 37.121014, 120.247050, 37.125346, 120.239470, 37.132259, 120.234024, 37.120544, 120.223936, 37.116090, 120.231932, 37.104714, 120.231702, 37.099437, 120.217395, 37.090740, 120.219863, 37.073514, 120.211114, 37.058335, 120.214543, 37.049935, 120.202262, 37.041793, 120.191591, 37.042995, 120.176384, 37.039030, 120.173184, 37.034647, 120.174348, 37.027575, 120.161969, 37.018088, 120.151497, 37.018469, 120.139601, 37.027624, 120.117690, 37.022032, 120.081704, 37.024249, 120.073556, 37.022118, 120.054163, 37.025176, 120.048153, 37.022140, 120.046836, 37.016287, 120.037803, 37.018387, 120.024525, 37.010327, 120.005835, 37.011631, 119.984254, 37.021559, 119.976236, 37.017995, 119.965242, 37.017984, 119.943632, 37.009588, 119.937889, 37.004452, 119.916059, 36.998091, 119.885038, 37.006863, 119.880289, 37.003152, 119.867880, 37.006507, 119.820897, 37.009235, 119.804014, 37.016340, 119.772007, 37.006456, 119.760079, 36.996886, 119.744985, 36.998024, 119.732042, 36.994423, 119.708664, 37.007228, 119.680002, 37.009216, 119.636936, 37.018476, 119.609923, 37.017484, 119.605314, 37.004036, 119.597052, 36.993475, 119.583832, 36.956178, 119.597548, 36.932938, 119.600492, 36.918798, 119.596676, 36.859811, 119.567349, 36.809726, 119.537422, 36.791192, 119.532044, 36.780506, 119.536012, 36.749437, 119.546871, 36.732863, 
	  119.561794, 36.726965, 119.567800, 36.719198, 119.579852, 36.718512, 119.587605, 36.702167, 119.597504, 36.695843, 119.608250, 36.673974, 119.614070, 36.669894, 119.622769, 36.647930, 119.645857, 36.635246, 119.653108, 36.627498, 119.658020, 36.629438, 119.684060, 36.612079, 119.697790, 36.611565, 119.705691, 36.601728, 119.714603, 36.600076, 119.756011, 36.571769, 119.766616, 36.570254, 119.766596, 36.567436, 119.782267, 36.561433, 119.829255, 36.546837, 119.919379, 36.532064, 119.930456, 36.525826, 119.932317, 36.502965, 119.942326, 36.517742, 119.952849, 36.525377, 119.976099, 36.521620, 120.009321, 36.503201, 120.010728, 36.491572, 120.004067, 36.482666, 120.012396, 36.468787, 120.012277, 36.460831, 120.007192, 36.456119, 120.001717, 36.459324, 119.969083, 36.459599, 119.968634, 36.454512, 119.960313, 36.453754, 119.954734, 36.449648, 119.950710, 36.452116, 119.937470, 36.429070, 119.926509, 36.423147, 119.931548, 36.401490, 119.942069, 36.396695, 119.945355, 36.391871, 119.905609, 36.387335, 119.905721, 36.375649, 119.899706, 36.372851, 119.901618, 36.366603, 119.897325, 36.354517, 119.899938, 36.349389, 119.893333, 36.325086, 119.867964, 36.316078, 119.864045, 36.308605, 119.855732, 36.308402, 119.851390, 36.299998, 119.835252, 36.284494, 119.830699, 36.264521, 119.820677, 36.262163, 119.822264, 36.250080, 119.815994, 36.249228, 119.811400, 36.244601, 119.816880, 36.219858, 119.830677, 36.190821, 119.817818, 36.175951, 119.773321, 36.173568, 119.748779, 36.165580, 119.703999, 36.184504, 119.680791, 36.186574, 119.669241, 36.174849, 119.652590, 36.147506, 119.651912, 36.139329, 119.647299, 36.134968, 119.655840, 36.106630, 119.635248, 36.099045, 119.632392, 36.086382, 119.639176, 36.076155, 119.663472, 36.069054, 119.674993, 36.070512, 119.690450, 36.060865, 119.709526, 36.060185, 119.717781, 36.049647, 119.711816, 36.044405, 119.709189, 36.034661, 119.700439, 36.032085,
	  119.697943, 36.027170, 119.688237, 36.022348, 119.692813, 35.999995, 119.683188, 35.988365, 119.692487, 35.963285, 119.702236, 35.948852, 119.703137, 35.933025, 119.713067, 35.931591, 119.717704, 35.927696, 119.720994, 35.912127, 119.727952, 35.904802, 119.732485, 35.873757, 119.729515, 35.870780, 119.721498, 35.870768, 119.719760, 35.863732, 119.698489, 35.871039, 119.689182, 35.868006, 119.676704, 35.847776, 119.666490, 35.847445, 119.660359, 35.853985, 119.654109, 35.854876, 119.627333, 35.838082, 119.620019, 35.823044, 119.611451, 35.819072, 119.609813, 35.809506, 119.617233, 35.797286, 119.617343, 35.789079, 119.599983, 35.781010, 119.593210, 35.764116, 119.595693, 35.756159, 119.616459, 35.742537, 119.628434, 35.724043, 119.608132, 35.718083, 119.591734, 35.720613, 119.571675, 35.718520, 119.559035, 35.727658, 119.547581, 35.730245, 119.523767, 35.726968, 119.519531, 35.722658, 119.514001, 35.702501, 119.525860, 35.681314, 119.527439, 35.650164, 119.524027, 35.638041, 119.517281, 35.632445, 119.523648, 35.616671, 119.538384, 35.607485, 119.537923, 35.599458, 119.561043, 35.600105, 119.573431, 35.590907, 119.579090, 35.599627, 119.591866, 35.606271, 119.606254, 35.599663, 119.619701, 35.608839, 119.632452, 35.607336, 119.648874, 35.596717, 119.667985, 35.591395, 119.750938, 35.533758, 119.864654, 35.558427, 119.901689, 35.578042, 119.963970, 35.636735, 120.000302, 35.681808, 120.045341, 35.692199, 120.129116, 35.701472, 120.192944, 35.720522, 120.226090, 35.783581, 120.248035, 35.838643, 120.263121, 35.848286, 120.373042, 35.875051, 120.469467, 35.920226, 120.496092, 35.936398, 120.515872, 35.939536, 120.568503, 35.964997, 120.619652, 36.015623, 120.678316, 36.036004, 120.752341, 36.030442, 120.784833, 35.980325, 120.827479, 35.866356, 120.890142, 35.866944, 120.928280, 35.884525, 120.915631, 35.932944, 120.836718, 35.990300, 120.741848, 36.068825, 120.737220, 36.167294, 
	  120.761058, 36.187707, 120.812524, 36.189639, 120.956526, 36.175775, 120.970700, 36.181718, 121.249703, 36.187536, 121.357879, 36.201383, 121.382519, 36.220268, 121.407905, 36.264648, 121.404163, 36.280492, 121.376139, 36.295312, 121.284367, 36.325784
	};
	public GetCord()
	{
		for ( int i = 0; i < areaArr.length; i++ )
		{
			if ( i % 2 == 0 )
			{lngArr.add( areaArr[i] );}
			else 
			{ latArr.add( areaArr[i] );}
		}
	}


	public static void main( String[] args )
	{
		double	lng	= 120.896137;
		double	lat	=36.561742;
		GetCord cord= new GetCord();
		boolean flag= cord.isInQdArea(lat, lng);
		System.out.println( flag );
	}
	

	/**
	 * 判断一个经纬度坐标是否在青岛市以内
	 * @param lat  纬度
	 * @param lng 经度
	 * @return true在青岛，false不在青岛
	 */
	public boolean isInQdArea( double lat, double lng )
	{
		boolean flag = isPointInPolygon( lng, lat, lngArr, latArr );
		return(flag);
	}


/**
 * 判断目标点是否在多边形内(由多个点组成)<br/>
 *
 * @param px
 *            目标点的经度坐标
 * @param py
 *            目标点的纬度坐标
 * @param polygonXA
 *            多边形的经度坐标集合
 * @param polygonYA
 *            多边形的纬度坐标集合
 * @return
 */
	private boolean isPointInPolygon( double px, double py,ArrayList<Double> polygonXA, ArrayList<Double> polygonYA )
	{
		boolean isInside= false;
		double	ESP	= 1e-9;
		int	count= 0;
		double	linePoint1x;
		double	linePoint1y;
		double	linePoint2x = 180;
		double	linePoint2y;

		linePoint1x	= px;
		linePoint1y	= py;
		linePoint2y	= py;

		for ( int i = 0; i < polygonXA.size() - 1; i++ )
		{
			double	cx1	= polygonXA.get( i );
			double	cy1	= polygonYA.get( i );
			double	cx2	= polygonXA.get( i + 1 );
			double	cy2	= polygonYA.get( i + 1 );
			/* 如果目标点在任何一条线上 */
			if ( isPointOnLine( px, py, cx1, cy1, cx2, cy2 ) )
			{
				return(true);
			}
			/* 如果线段的长度无限小(趋于零)那么这两点实际是重合的，不足以构成一条线段 */
			if ( Math.abs( cy2 - cy1 ) < ESP )
			{
				continue;
			}
			/* 第一个点是否在以目标点为基础衍生的平行纬度线 */
			if ( isPointOnLine( cx1, cy1, linePoint1x, linePoint1y, linePoint2x,
					    linePoint2y ) )
			{
				/* 第二个点在第一个的下方,靠近赤道纬度为零(最小纬度) */
				if ( cy1 > cy2 )
					count++;
			}
			/* 第二个点是否在以目标点为基础衍生的平行纬度线 */
			else if ( isPointOnLine( cx2, cy2, linePoint1x, linePoint1y,
						 linePoint2x, linePoint2y ) )
			{
				/* 第二个点在第一个的上方,靠近极点(南极或北极)纬度为90(最大纬度) */
				if ( cy2 > cy1 )
					count++;
			}
			/* 由两点组成的线段是否和以目标点为基础衍生的平行纬度线相交 */
			else if ( isIntersect( cx1, cy1, cx2, cy2, linePoint1x, linePoint1y,
					       linePoint2x, linePoint2y ) )
			{
				count++;
			}
		}
		if ( count % 2 == 1 )
		{
			isInside = true;
		}

		return(isInside);
	}


/**
 * 是否有 横断
 *
 * @param px1
 * @param py1
 * @param px2
 * @param py2
 * @param px3
 * @param py3
 * @param px4
 * @param py4
 * @return
 */
	private boolean isIntersect( double px1, double py1, double px2, double py2, double px3, double py3, double px4, double py4 )
	{
		boolean flag	= false;
		double	d	= (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);
		if ( d != 0 )
		{
			double r = ( (py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3) )
				   / d;
			double s = ( (py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1) )
				   / d;
			if ( (r >= 0) && (r <= 1) && (s >= 0) && (s <= 1) )
			{
				flag = true;
			}
		}
		return(flag);
	}


/**
 * 目标点是否在目标边上边上<br/>
 *
 * @param px0
 *            目标点的经度坐标
 * @param py0
 *            目标点的纬度坐标
 * @param px1
 *            目标线的起点(终点)经度坐标
 * @param py1
 *            目标线的起点(终点)纬度坐标
 * @param px2
 *            目标线的终点(起点)经度坐标
 * @param py2
 *            目标线的终点(起点)纬度坐标
 * @return
 */
	private boolean isPointOnLine( double px0, double py0, double px1,double py1, double px2, double py2 )
	{
		boolean flag	= false;
		double	ESP	= 1e-9; /* 无限小的正数 */
		if ( (Math.abs( Multiply( px0, py0, px1, py1, px2, py2 ) ) < ESP)
		     && ( (px0 - px1) * (px0 - px2) <= 0)
		     && ( (py0 - py1) * (py0 - py2) <= 0) )
		{
			flag = true;
		}
		return(flag);
	}


	private double Multiply( double px0, double py0, double px1, double py1,
				double px2, double py2 )
	{
		return( (px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0) );
	}
}

